/**
 * 
 */
package com.worksmap.core.action.rate;

import javax.servlet.http.HttpServletRequest;

import net.sf.json.JSONObject;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.sdicons.json.mapper.MapperException;
import com.worksmap.core.action.BaseController;
import com.worksmap.core.entity.SellerConfig;
import com.worksmap.core.entity.TaoBaoSeller;
import com.worksmap.core.service.SellerConfigService;
import com.worksmap.core.service.UserService;
import com.worksmap.core.task.RateTask;
import com.worksmap.core.task.RateTradeExecutor;
import com.worksmap.core.util.JsonUtil;
import com.worksmap.core.util.StringUtil;

/**
 * @author jack
 *
 */
@Controller
@RequestMapping("/sellerConfig")
public class SellerConfigController extends BaseController {
	
	Logger logger = Logger.getLogger(this.getClass());
	
	@Autowired
	SellerConfigService sellerConfigService;
	@Autowired
	UserService userService;
	
	/**
	 * 方法简介：获取自动评价设置
	 * jack
	 * 2013-12-21
	 */
	@RequestMapping(value = "", method = RequestMethod.GET)
	public Object getSellerConfig(HttpServletRequest request){
//		TaoBaoSeller seller = new TaoBaoSeller();
//		seller.setAccess_token(RateConstant.sessionKey);
		TaoBaoSeller seller = userService.getCurrentSeller(request);
		SellerConfig sellerConfig = sellerConfigService.getConfig(seller.getTaobao_user_id());
		logger.info(sellerConfig);
		request.setAttribute("sellerConfig", sellerConfig);
		return "rate/setup";
	}
	
	@RequestMapping(value = "updateConfig", method = RequestMethod.POST)
	@ResponseBody
	public Object updateConfig(SellerConfig config, HttpServletRequest request) throws MapperException{
		TaoBaoSeller seller = userService.getCurrentSeller(request);
		SellerConfig sellerConfig = sellerConfigService.getConfig(seller.getTaobao_user_id());
		sellerConfig.setTrade_rate_condition(config.getTrade_rate_condition());
		sellerConfig.setBlack_list_switch(config.getBlack_list_switch());
		sellerConfig.setGrab_switch(config.getGrab_switch());
		boolean status = sellerConfigService.updateSellerConfig(sellerConfig);
		JSONObject json = new JSONObject();
		json.put("status", status ? 1 : 0);
		json.put("sellerConfig", JsonUtil.objectToJsonStr(sellerConfig));
		return json.toString();
	}
	
	/**
	 * 方法简介：自动评价开关
	 * 如果是开启自动评价，则开启一个线程扫描一遍是否有需要评价的订单，如果有则评价，如无则退出线程；
	 * 当线程评价结束后，线程相应结束，则以后需要评价的订单由消息服务处理，
	 * 消息服务接收到交易成功或买家评价的消息，则判断卖家是否开启自动评价，若开启则直接处理评价
	 * jack
	 * 2013-12-21
	 * @throws MapperException 
	 */
	@RequestMapping(value = "control", method = RequestMethod.POST)
	@ResponseBody
	public Object control(String controlValue, HttpServletRequest request) throws MapperException{
		logger.info(controlValue);
		TaoBaoSeller seller = userService.getCurrentSeller(request);
		SellerConfig sellerConfig = new SellerConfig(seller.getTaobao_user_id());
		if(StringUtil.isNotBlank(controlValue) && "true".equalsIgnoreCase(controlValue)){
			sellerConfig = sellerConfigService.getConfig(seller.getTaobao_user_id());
			sellerConfig.setAuto_rate_switch(1);
			//启动自动评价
			RateTradeExecutor.executor.execute(new RateTask(
					seller, sellerConfig, request.getSession().getServletContext()));
		}
		request.getSession().getServletContext();
		boolean status = sellerConfigService.updateSellerConfigAutoSwitch(sellerConfig);
		JSONObject json = new JSONObject();
		json.put("status", status ? 1 : 0);
		json.put("sellerConfig", JsonUtil.objectToJsonStr(sellerConfig));
		return json.toString();
	}

}
